#! /bin/sh -vx
# $Id$
# Copyright 2023 Andreas Scherer <tex-live@tug.org>
# You may freely use, modify and/or distribute this file.

BinDir=${BinDir:-.}
ExeExt=${ExeExt:-}
_ctwill=$BinDir/ctwill$ExeExt
_ctwill_refsort=$BinDir/ctwill-refsort$ExeExt
_ctwill_twinx=$BinDir/ctwill-twinx$ExeExt

# pre-generated test results in the repository are stored in LF
# but the output might be written in CRLF on some platform.
# if 'diff --strip-trailing-cr' is available, exploit it.
# (useful for tests on win32 binaries run on MSYS shell)
DIFF="diff"
$DIFF --strip-trailing-cr $0 $0 \
  && DIFF="diff --strip-trailing-cr" || echo

testdir=$srcdir/cwebdir/tests

# Test CTWILL by running it twice
CWEBINPUTS=$testdir $_ctwill ham ham || exit 1
CWEBINPUTS=$testdir $_ctwill ham ham || exit 2
diff ham.tex $testdir/ham.tex || exit 3

# Test CTWILL-REFSORT
$_ctwill_refsort < $testdir/ham.ref > ham.sref || exit 4
$DIFF ham.sref $testdir/ham.sref || exit 5

# Test CTWILL-TWINX
$_ctwill_twinx ham.tex > index.tex
$DIFF index.tex $testdir/index.tex || exit 6

# Test CTWILL-PROOFSORT
# Perl script not available in builddir, and requires Unicode::Collate;
# if that module is not available, just quit testing.
perl -e 'use Unicode::Collate; Unicode::Collate->new();' 2>/dev/null || exit 0
$testdir/../ctwill-proofsort < ham.tex > ham-sorted.tex || exit 7
$DIFF ham-sorted.tex $testdir/ham-sorted.tex || exit 8

exit 0
